home *** CD-ROM | disk | FTP | other *** search
- /* mandel.c -- main routine for Mandelbrot UNIX daemon.
- *
- * %W%
- *
- * Author: Scott Mulligan
- * Copyright 1993,1994 Apple Computer, Inc.
- * All Rights Reserved.
- *
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF APPLE COMPUTER, INC.
- * The copyright notice above does not evidence any actual or
- * intended publication of such source code.
- */
-
- #include <stdio.h>
- #include <netdb.h> /* */
- #include <sys/errno.h> /* */
- /* #include <sys/id.h> /* */
- #include <sys/types.h> /* */
- #include <sys/socket.h> /* */
- #include <netinet/in.h> /* */
- /* #include <pwd.h> /* */
- /* #include <unistd.h> /* */
- /* #include <sys/access.h> /* */
-
- #include "Types.h"
- #include "AppleEvents.h"
- #include "AUXAESuite.h"
- #include "mandel.h"
- /* #include "UAppleEvents.h" */
- #include <sys/file.h>
- #include <sys/wait.h>
- #include <signal.h> /* */
-
- /* Global variable declarations */
-
- AEAddressDesc target;
-
- int debuglevel=0;
- int sessionid=0;
- int missed_heartbeats=0;
- int theInterval=0;
- boolean sendconfig=FALSE;
- boolean TimeToQuit=FALSE;
- boolean pending_cmd=FALSE;
- char qcmds[4][128];
- int ncmds=0;
-
- char *databuf;
- char OSVersion[32];
- char *clientVersion;
- char *serverVersion;
-
- time_t time_of_last_recv; /* last heartbeat received */
- time_t time_of_last_check; /* last time we checked how long since we recv'd */
- time_t time_of_last_send; /* last time we sent a heartbeat */
- time_t time_of_last_update; /* last time we sent an update */
-
- FILE *debugfp;
-
-
-
- /* External variable declarations */
-
- extern int errno;
-
-
-
- /* The AEHandler structure */
-
- typedef struct _AEHandler {
- AEEventID theAEEventID;
- AEEventHandlerProcPtr handler;
- } AEHandler;
-
-
-
- /* The "AppleEvents" handler table */
-
- AEHandler handlerTable[] = {
- { kAENull,
- HandleNullEvent },
- { kAEQuit,
- HandleQuit },
- { kAEHeartBeat,
- HandleHeartBeat },
- { kAEVersion,
- HandleVersionEvent },
- { kAEMandel,
- HandleMandel }
- };
-
-
-
-
- void setup_handler_tbl()
- {
- int i, result;
-
- AEInit();
-
- for (i = 0; i < sizeof(handlerTable) / sizeof(AEHandler); i++) {
- result = AEInstallEventHandler(kAEAUXSuite,
- handlerTable[i].theAEEventID,
- handlerTable[i].handler,
- 0L, false);
- }
-
- }
-
-
-
- reap_child()
- {
- int status, pid;
-
- #ifdef DEBUG
- fprintf(debugfp,"reap_child(): At the top.\n");
- fflush(debugfp);
- #endif
- pid=wait3(&status, WNOHANG,0);
- #ifdef DEBUG
- fprintf(debugfp,"reap_child(): At the bottom.\n");
- fflush(debugfp);
- #endif
- return(0);
- }
-
-
- /*
- * This is the main() routine for mandeld. All of the daemon startup work
- * has already been done for us by ppcd following the standard UNIX
- * inetd model.
- *
- * We inherit file descriptor 0 from ppcd. It is an active TCP socket
- * connection to the client application.
- */
-
- void main(argc, argv)
- int argc;
- char** argv;
- {
- OSErr result;
- EventRecord event;
- int i,num,time_out;
- int six_oh_eight=0;
- time_t time_of_select, time_now;
-
- chdir ("/tmp"); /* so core files end up in /tmp */
-
- setup_debug(argv[0]);
-
- set_access(argc,argv);
-
- setup_handler_tbl(v);
-
- signal(SIGCHLD,reap_child);
-
- time_of_last_recv = 0;
- time_of_last_send = 0;
- time_of_last_check = 0;
- time_of_last_update = 0;
-
-
- #ifdef SLEEP
- #ifdef DEBUG
- fprintf(debugfp,"MANDELD: Sleeping 60 seconds to allow gdb attach...\n");
- #endif
- sleep(60);
- #endif
-
-
- (void)time(&time_of_last_recv);
- (void)time(&time_of_last_send);
-
-
- for (;;) {
-
- (void)time(&time_now);
- if ((time_of_last_check + HEARTBEAT_CHECK - time_now) <
- (time_of_last_send + HEARTBEAT_SEND - time_now))
- time_out = time_of_last_check + HEARTBEAT_CHECK - time_now;
- else
- time_out = time_of_last_send + HEARTBEAT_SEND - time_now;
-
- if (theInterval > 0)
- if ((time_of_last_update + theInterval - time_now) < time_out)
- time_out = time_of_last_update + theInterval - time_now;
-
- if (time_out <= 0) time_out = 1;
-
- result = WaitNextAppleEvent (&event, &time_out);
- if (result == -1) {
- #ifdef DEBUG
- fprintf (debugfp,"main(): Looks like the client quit...\n");
- fflush(debugfp);
- #endif
- TimeToQuit = TRUE;
- }
- if (result > 0)
- result = AEProcessAppleEvent (&event);
-
- if (TimeToQuit == TRUE) shutdown_now();
- (void)time(&time_now);
- if ((time_now - time_of_last_send ) >= HEARTBEAT_SEND ) snd_heartbeat();
- if ((time_now - time_of_last_check) >= HEARTBEAT_CHECK) chk_heartbeat();
-
- #ifdef DEBUG
- fprintf (debugfp,"main(): Bottom of main loop.\n");
- fflush(debugfp);
- #endif
- }
-
- exit(0);
- }
-
- /*** EOF ***/
-